<?php

namespace App\Kuafu\ProductMenu\Validate;

use App\Exceptions\CustomValidateException;
use App\Http\Validate\AbstractBaseValidate;
use App\Kuafu\ProductMenu\Model\ProductMenu;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Rule;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ProductMenuV2Validate extends AbstractBaseValidate
{
    /**
     * @inheritDoc
     */
    function getRules(): array
    {
        // TODO: Implement getRules() method.
        return [];
    }

    /**
     * @inheritDoc
     */
    function getAttributes(): array
    {
        // TODO: Implement getAttributes() method.
        return [];
    }

    /**
     * @inheritDoc
     */
    function registerValidateRules(): void
    {
        // TODO: Implement registerValidateRules() method.
    }

    /**
     * @inheritDoc
     */
    function getMessages(): array
    {
        // TODO: Implement getMessages() method.
        return [];
    }

    /**
     * 置顶校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws CustomValidateException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public static function topValidate(array $params)
    {
        return self::getInstance()->validate(
            ["type", "menuId"],
            $params,
            [
                "type"   => "required|int|in:0,1",
                "menuId" => [
                    "required",
                    "int",
                    Rule::exists((new ProductMenu())->getTable(), "id")
                ]
            ]
        );
    }

    /**
     * 菜单批量下发校验
     *
     * @param array $params
     * @return MessageBag|object
     * @throws ContainerExceptionInterface
     * @throws CustomValidateException
     * @throws NotFoundExceptionInterface
     */
    public static function batchAddValidate(array $params)
    {
        $fields = [
            "name",
            "desc",
            "price",
            "costPrice",
            "vipPrice",
            "isHot",
            "isRecommend",
            "showPlatform"
        ];
        return self::getInstance()->validate(
            ["fields"],
            $params,
            [
                "fields"   => "required|array|min:1",
                "fields.*" => [
                    "required",
                    "string",
                    "in:" . implode(",", $fields)
                ]
            ]
        );
    }
}
